package tag;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import access.AccessFactory;
import access.service.AccessService;

public class ActionTag extends SimpleTagSupport {
	private int actionId;//动作Id

	/**
	 * JSP自定义标签处理对象
	 */
	@Override
	public void doTag() throws JspException, IOException {
		//获取session中的属性
		Integer userId = (Integer) getJspContext().getAttribute("userId", PageContext.SESSION_SCOPE);
		
		if(userId != null) {
			//数据访问对象
			AccessService dao = AccessFactory.getAccess();
			
			//具有该action权限则数据标签体内容
			if(dao.accessAction(userId, actionId)) {
				//JSP输出流
				JspWriter out = getJspContext().getOut();
				//标签内容缓存
				StringWriter sw = new StringWriter();
				//获得标签内容
				getJspBody().invoke(sw);
				//输入标签内容
				out.print(sw.toString());
			}
		}
	}

	//getter setter
	public int getActionId() {
		return actionId;
	}
	public void setActionId(int actionId) {
		this.actionId = actionId;
	}
}
